package com.blog.config;

import com.blog.quartz.ScoreRefreshJob;
import com.blog.quartz.ViewCountJob;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;

import javax.annotation.Resource;

/**
 * @author 任浩
 * @version 1.0
 */

// 配置 -》数据库 -》调用
@Configuration
public class QuartzConfig {

//    @Autowired
//    private ScoreRefreshJob scoreRefreshJob;
//
//    @Autowired
//    private ViewCountJob viewCountJob;

    //FactoryBean可以简化对象的实列化过程：
    //1、通过FactoryBean封装Bean的实例化过程
    //2、将FactoryBean封装到spring容器里面.
    //3、将FactoryBean的值注入给其他的bena.
    //4、其他Bean得到的是该FactoryBean管理的实例对象。

    /**
     * 这种方式生成bean的默认的名称为方法的名称，我们可以根据@Qualifier注解根据bean名称来匹配bean。
     * @return
     */
    @Bean(name = "scoreJobDetail")
    public JobDetailFactoryBean scoreJobDetail(){
        JobDetailFactoryBean detailFactoryBean = new JobDetailFactoryBean();
        detailFactoryBean.setJobClass(ScoreRefreshJob.class);  //设置具体的线程池执行的业务的类。
//        detailFactoryBean.setBeanName("scoreJob");
        detailFactoryBean.setName("ScoreJob");
        detailFactoryBean.setGroup("ScoreGroup");
        detailFactoryBean.setDurability(true); //设置是否持久化。
        detailFactoryBean.setRequestsRecovery(true);
        return  detailFactoryBean;
    }
//    @Bean(name = "scoreTrigger")
//    public SimpleTriggerFactoryBean scoreTrigger(@Qualifier("scoreJobDetail") JobDetail scoreJobDetail){
//        SimpleTriggerFactoryBean simpleTriggerFactoryBean = new SimpleTriggerFactoryBean();
//        simpleTriggerFactoryBean.setJobDetail(scoreJobDetail);
//        simpleTriggerFactoryBean.setName("ScoreTrigger");
//        simpleTriggerFactoryBean.setGroup("ScoreTriggerGroup");
//        simpleTriggerFactoryBean.setRepeatInterval(60000); //设置执行的频率，就是定时的间隔。
//        simpleTriggerFactoryBean.setJobDataMap(new JobDataMap());
//        return simpleTriggerFactoryBean;
//    }
    //定义定时更新浏览量到数数据库的定时任务。
    @Bean(name = "viewCountJobDetail")
    public JobDetailFactoryBean viewCountJobDetail(){
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setJobClass(ViewCountJob.class);
//        jobDetailFactoryBean.setBeanName("viewCountJob");
        jobDetailFactoryBean.setName("viewCount");
        jobDetailFactoryBean.setGroup("viewCountGroup");
        jobDetailFactoryBean.setDurability(true); //设置是否持久化。
        jobDetailFactoryBean.setRequestsRecovery(true);
        return jobDetailFactoryBean;
    }
//    @Bean(name = "viewCountTrigger")
//    public SimpleTriggerFactoryBean viewCountTrigger(@Qualifier("viewCountJobDetail") JobDetail jobDetailViewCount){
//        SimpleTriggerFactoryBean simpleTriggerFactoryBean = new SimpleTriggerFactoryBean();
//        simpleTriggerFactoryBean.setJobDetail(jobDetailViewCount);
//        simpleTriggerFactoryBean.setName("viewCountTrigger");
//        simpleTriggerFactoryBean.setGroup("viewCountTriggerGroup");
//        simpleTriggerFactoryBean.setRepeatInterval(60000); //设置执行的频率，就是定时的间隔。
//        simpleTriggerFactoryBean.setJobDataMap(new JobDataMap());  //new一个为null的对象，代表执行定时任务我们不需要传入参数
//        return simpleTriggerFactoryBean;
//    }


    @Bean(name = "scoreTrigger")
    public CronTriggerFactoryBean scoreJobTrigger(@Qualifier("scoreJobDetail") JobDetailFactoryBean scoreJobDetail) {
        CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
        trigger.setJobDetail(scoreJobDetail.getObject()); // 注意这里获取的是JobDetail对象，而不是FactoryBean
        trigger.setCronExpression("0 0/5 * * * ?"); // 每五分钟执行一次
        trigger.setName("ScoreJobTrigger");
        trigger.setGroup("ScoreGroup");
        return trigger;
    }

    @Bean(name = "viewCountTrigger")
    public CronTriggerFactoryBean viewCountJobTrigger(@Qualifier("viewCountJobDetail") JobDetailFactoryBean viewCountJobDetail) {
        CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
        trigger.setJobDetail(viewCountJobDetail.getObject()); // 同样，获取JobDetail对象
        trigger.setCronExpression("0 0 2 * * ?"); // 每天凌晨两点执行一次
        trigger.setName("ViewCountJobTrigger");
        trigger.setGroup("viewCountGroup");
        return trigger;
    }
}
